﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace HelperPrototype.OtherForms
{
    public partial class SuaPhieuMuaHang : Form
    {
        private string _maPhieu;


        public SuaPhieuMuaHang(string MaPhieuMuaHang)
        {
            InitializeComponent();
            _maPhieu = MaPhieuMuaHang;
        }

        private void SuaPhieuMuaHang_Load(object sender, EventArgs e)
        {
            loadData();
        }

        private void loadData()
        {
            _DB.DataClasses1DataContext dc = new _DB.DataClasses1DataContext();
            _DB.PhieuMuaHang p = dc.PhieuMuaHangs.Single(x => x.MaPhieu == _maPhieu);

            txtMaPhieu.Text = p.MaPhieu;
            txtContent.Text = p.NoiDung;

            dgvHangHoa.Rows.Clear();
            int count = 1;
            /*
            foreach (_DB.PhieuMuaHang_HangHoa h in p.PhieuMuaHang_HangHoas)
            {
                DataGridViewRow r = new DataGridViewRow();
                r.CreateCells(dgvHangHoa);
                r.Cells[0].Value = count++;
                r.Cells[1].Value = h.HangHoa_NhaCungCap.HangHoa.TenHangHoa;
                r.Cells[2].Value = h.SoLuong.ToString();
                r.Cells[3].Value = h.HangHoa_NhaCungCap.NhaCungCap.TenNhaCungCap;
                r.Cells[4].Value = h.HangHoa_NhaCungCap.Gia.ToString();

                r.Tag = h;
                r.Cells[0].Tag = h.HangHoa_NhaCungCap_ID;

                dgvHangHoa.Rows.Add(r);
            }*/
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            /*
            _DB.DataClasses1DataContext dc = new _DB.DataClasses1DataContext();
            _DB.PhieuMuaHang p = dc.PhieuMuaHangs.Single(x => x.MaPhieu == _maPhieu);

            p.NoiDung = txtContent.Text;
            for (int i = 0; i < dgvHangHoa.Rows.Count; i++)
            {
                DataGridViewRow r = dgvHangHoa.Rows[i];
                p.PhieuMuaHang_HangHoas[i].HangHoa_NhaCungCap_ID = (int) r.Cells[0].Tag;
                p.PhieuMuaHang_HangHoas[i].SoLuong = float.Parse(r.Cells[2].Value as string);
            }

            dc.SubmitChanges();
            this.Close();*/
        }

        private void dgvHangHoa_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            /*
            if (e.RowIndex >= 0 && e.ColumnIndex == 3)
            {
                _DB.PhieuMuaHang_HangHoa h = (_DB.PhieuMuaHang_HangHoa)dgvHangHoa.Rows[e.RowIndex].Tag;
                string maHangHoa = h.HangHoa_NhaCungCap.MaHangHoa;
                OtherForms.chonHangHoaNhaCCForm f = new OtherForms.chonHangHoaNhaCCForm(maHangHoa);
                f.ShowDialog();

                if (f.getHangHoaNCCID() >= 0)
                {
                    _DB.DataClasses1DataContext dc = new _DB.DataClasses1DataContext();
                    _DB.HangHoa_NhaCungCap hn = 
                        dc.HangHoa_NhaCungCaps.Single(x => x.HangHoa_NhaCungCap_ID == f.getHangHoaNCCID());

                    dgvHangHoa.Rows[e.RowIndex].Cells[3].Value = hn.NhaCungCap.TenNhaCungCap;
                    dgvHangHoa.Rows[e.RowIndex].Cells[4].Value = hn.Gia;

                    dgvHangHoa.Rows[e.RowIndex].Cells[0].Tag = f.getHangHoaNCCID();
                }

            }*/
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }


    }
}
